<%--
  - Author		:	Jaewon Choi
  --%>
<%@ page language="java" %>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page pageEncoding="utf-8"%>
<%@ include file="/WEB-INF/pages/includes/taglibs.jspf" %>
<%@ include file="/WEB-INF/pages/includes/variables.jspf" %>
<%@ include file="/WEB-INF/pages/includes/doctype.jspf" %>
<c:choose>
	<c:when test="${not empty event}">
		<c:set var="fAct" value="수정" />
	</c:when>
	<c:otherwise>
		<c:set var="fAct" value="등록" />
	</c:otherwise>
</c:choose>
<html>
	<head>
<%@ include file="/WEB-INF/pages/includes/metatags.jspf" %>
		<title><fmt:message key="pages.title" /></title>
		<link rel="stylesheet" type="text/css" href="<c:url value="${dirStyles}/common.css" />" />

		<link rel="stylesheet" type="text/css" href="<c:url value="${dirScripts}/jqueryUi/css/ui-lightness/jquery-ui-1.8.21.custom.css" />" />

		<script type="text/javascript" src="<c:url value="${dirScripts}/jquery-1.7.2.min.js" />"></script>
		<script type="text/javascript" src="<c:url value="${dirScripts}/jquery-ui-1.8.21.custom.min.js" />"></script>
		<script type="text/javascript">
			$(function(){
				$("#name").focus();
				
				$( ".date" ).datepicker({
					showOn: "both",
					buttonImage: "<c:url value="${dirImages}/calendar.png" />",
					buttonImageOnly: true,
					dateFormat: "yy-mm-dd",
					showWeek: true
				});
			});
			
			function chkContNm(){
				var idx = "0";
				var name = document.frm.name.value;
				if(document.frm.idx){
					idx = document.frm.idx.value;
				}

				$.ajax({
					type: "POST",
					url: "<c:url value="/event/chkDupl" />",
					dataType: "json",
					async: false,
					data: {
								"idx" : idx,
								"chkName" : name
							},
					success: function(data){
						if(data.resultCode == "200"){
							chkContNmFlag = data.result;
							if(eval(chkContNmFlag)){
								$(document.frm.name).css("background", "#ADC900");
							}else{
								$(document.frm.name).css("background", "#F2685E");
							}
						}else{
							alert(data.errMsg);
						}
					},
					beforeSend: function(){
					},
					error: function(jqXHR, textStatus, errorThrown){
						//alert(jqXHR.responseText); //에러 페이지 내용
						alert("오류가 발생하였습니다.\n관리자에게 문의 바랍니다.");
						throw errorThrown;
					},
					complete: function(){
					}
				 });
				return eval(chkContNmFlag);
			}

			function validation(){
				<c:if test="${param.fAct eq 'view'}">
					return true;
				</c:if>
				try{
					if($(".categoryIdx option:selected").val().replace(/\s/g, '') == "") {
						alert("[브랜드]은 필수 선택 항목입니다.");
						document.frm.categoryIdx.focus();
						return false;
					}
					
					if((document.frm.name.value).replace(/\s/g, '') == ""){
						alert("[이벤트명]은 필수 입력 항목입니다.");
						document.frm.name.focus();
						return false;
					}
					
					if(! chkContNm()){
						alert("[이벤트명]은 중복될 수 없습니다.");
						document.frm.name.focus();
						return false;
					}
					<c:if test="${empty event}">
						if((document.frm.bigBannerFile.value).replace(/\s/g, '') == ""){
							alert("[빅배너 이미지]는 필수 입력 항목입니다.");
							document.frm.bigBannerFile.focus();
							return false;
						}
						if((document.frm.smallBannerFile.value).replace(/\s/g, '') == ""){
							alert("[스몰배너 이미지]는 필수 입력 항목입니다.");
							document.frm.smallBannerFile.focus();
							return false;
						}
						if((document.frm.detailFile.value).replace(/\s/g, '') == ""){
							alert("[상세 이미지]는 필수 입력 항목입니다.");
							document.frm.detailFile.focus();
							return false;
						}
					</c:if>

				}catch(err){
					return false;	
				}
				
				return confirm("${fAct} 하시겠습니까?");
			}
		</script>
	</head>
	<body>
		<div class="bodyWrap">
<%@ include file="/WEB-INF/pages/includes/header.jsp" %>
<%@ include file="/WEB-INF/pages/includes/left.jsp" %>
			<div class="contWrap">
				<div class="menuTitle">
					이벤트${fAct}
				</div>
				<form id="frm" name="frm" method="post" enctype="multipart/form-data" action="<c:url value="/event/submit.do" />" onsubmit="return validation();">
					<input type="hidden" name="maxnum" value="${param.maxnum}" />
					<input type="hidden" name="page" value="${param.page}" />
					<input type="hidden" name="searchDate" value="${param.searchDate}">
					<input type="hidden" name="startDate" value="${param.startDate}" /> 
					<input type="hidden" name="endDate" value="${param.endDate}" />
					<input type="hidden" name="searchName" value="${param.searchName}" />
					
					<table class="frmTbl">
						<colgroup>
							<col width="176px;" />
							<col width="*" />
							<col width="100px;" />
							<col width="*" />
						</colgroup>
						<tbody>
			 				<c:if test="${not empty event}">
			 					<tr>
									<th>이벤트 IDX</th>
									<td colspan="2">
										${event.idx}
										<input type="hidden" name="idx" value="${event.idx}">
									</td>
								</tr>
			 				</c:if>
			 				<tr>
								<th>브랜드 *</th>
								<td colspan="2">
									<select class="categoryIdx" name="categoryIdx">
										<option value="">선택</option>
										<c:forEach var="category" items="${categoryList}">
											<option value="${category.idx}" <c:if test="${event.categoryIdx eq category.idx}"> selected="selected" </c:if>>${category.name}</option>
										</c:forEach>
									</select>
								</td>
							</tr>
			 				<tr>
								<th>기간</th>
								<td colspan="2">
									<label>
										<input type="checkbox" <c:if test="${empty event.beginTime && empty event.endTime}"> checked="checked" </c:if> onclick="javascript:if(this.checked){$('.date').attr('disabled','disabled');}else{$('.date').removeAttr('disabled');};"> 항상
									</label>
									<c:catch var="e">
										<fmt:parseDate var="beginTime" pattern="yyyy-MM-dd HH:mm:ss" value="${event.beginTime}" />
										<fmt:formatDate var="beginTime" value="${beginTime}" pattern="yyyy-MM-dd" />
									</c:catch>
									<c:if test="${not empty e}">
										<c:set var="beginTime" value="${event.beginTime}" />
									</c:if>
									<c:catch var="e">
										<fmt:parseDate var="endTime" pattern="yyyy-MM-dd HH:mm:ss" value="${event.endTime}" />
										<fmt:formatDate var="endTime" value="${endTime}" pattern="yyyy-MM-dd" />
									</c:catch>
									<c:if test="${not empty e}">
										<c:set var="endTime" value="${event.endTime}" />
									</c:if>
									<input type="text" class="date" name="beginTime" <c:if test="${empty event.beginTime && empty event.endTime}"> disabled="disabled" </c:if> readonly="readonly" value="${beginTime}" />
									~
									<input type="text" class="date" name="endTime" <c:if test="${empty event.beginTime && empty event.endTime}"> disabled="disabled" </c:if> readonly="readonly" value="${endTime}" />
								</td>
							</tr>
							<tr>
								<th>이벤트명 *</th>
								<td colspan="2">
									<input type="text" id="name" name="name" value="${event.name}" onfocus="javascript:chkContNmFlag=false;" maxlength="15" title="이벤트명 입니다." />
								</td>
							</tr>
							<input type="hidden" name="emblem" value="X" />
							<!-- <tr>
								<th>엠블럼 *</th>
								<td colspan="2">
									<select name="emblem">
										<option value="X">없음</option>
										<option value="N" <c:if test="${event.emblem eq 'N'}"> selected="selected" </c:if>>NEW</option>
										<option value="E" <c:if test="${event.emblem eq 'E'}"> selected="selected" </c:if>>EVENT</option>
										<option value="S" <c:if test="${event.emblem eq 'S'}"> selected="selected" </c:if>>SPECIAL</option>
										<option value="H" <c:if test="${event.emblem eq 'H'}"> selected="selected" </c:if>>HOT</option>
									</select>
								</td>
							</tr> -->
							<tr>
								<th>빅배너 등록 *</th>
								<td colspan="2">
									<input type="file" id="bigBannerFile" name="eventFiles[0]" />
									<input type="hidden" name="eventFileTypes" value="bigBanner" />
									<input type="hidden" name="eventFileDatas" value="" />
									<c:if test="${not empty bigBanner[0].fileUrl}">
										<br /><img src="${bigBanner[0].fileUrl}" class="thumbnailImg" />
									</c:if>
								</td>
							</tr>
							<tr>
								<th>스몰배너 등록 *</th>
								<td colspan="2">
									<input type="file" id="smallBannerFile" name="eventFiles[1]" />
									<input type="hidden" name="eventFileTypes" value="smallBanner" />
									<input type="hidden" name="eventFileDatas" value="" />
									<c:if test="${not empty smallBanner[0].fileUrl}">
										<br /><img src="${smallBanner[0].fileUrl}" class="thumbnailImg" />
									</c:if>
								</td>
							</tr>
							<tr>
								<th>상세 이미지 등록 *</th>
								<td colspan="2">
									<input type="file" id="detailFile" name="eventFiles[2]" />
									<input type="hidden" name="eventFileTypes" value="detail" />
									<input type="hidden" name="eventFileDatas" value="" />
									<c:if test="${not empty detail[0].fileUrl}">
										<br /><img src="${detail[0].fileUrl}" class="thumbnailImg" />
									</c:if>
								</td>
							</tr>
							<tr>
								<th>이벤트 링크</th>
								<td colspan="2">
									<input type="text" name="linkUrl" style="width: 600px;" maxlength="200" value="${event.linkUrl}" placeholder="http://나 https://를 포함한 URL을 입력하세요." />
								</td>
							</tr>
							<input type="hidden" name="isMain" value="0" />
							<!-- <tr>
								<th>메인 노출 여부</th>
								<td colspan="2">
									<label>
										<input type="radio" name="isMain" value="1" <c:if test="${empty event or event.isMain eq 1}"> checked="checked" </c:if> /> Yes
										
									</label>
									<label>
										<input type="radio" name="isMain" value="0" <c:if test="${event.isMain eq 0}"> checked="checked" </c:if> /> No
									</label>
								</td>
							</tr>
							<tr>
								<th rowspan="11">메인 이미지</th>
								<th>이미지 파일(삭제할 이미지를 체크하세요.)</th>
								<th>엠블럼</th>
							</tr>
							<c:forEach var="mainImg" items="${mainImgs}" varStatus="stat">
								<tr>
									<td>
										<input type="checkbox" name="eventFileIdx" value="${mainImg.idx}" />
										<img src="${mainImg.fileUrl}" class="thumbnailImg" />
									</td>
									<td>
										<c:set var="emblem" value="${fn:split(mainImg.fileData, '^')[4]}" />
										<c:choose>
											<c:when test="${emblem eq 'N'}">NEW</c:when>
											<c:when test="${emblem eq 'E'}">EVENT</c:when>
											<c:when test="${emblem eq 'S'}">SPECIAL</c:when>
											<c:when test="${emblem eq 'H'}">HOT</c:when>
											<c:when test="${emblem eq 'X'}">없음</c:when>
											<c:otherwise>${emblem}</c:otherwise>
										</c:choose>
									</td>
								</tr>
							</c:forEach>
							<c:forEach var="i" begin="3" end="${12-fn:length(mainImgs)}" step="1" varStatus="stat">
								<tr>
									<td>
										<input type="file" id="coverFile" name="eventFiles[${i}]" />
										<input type="hidden" name="eventFileTypes" value="mainImg" />
										<input type="hidden" name="eventFileDatas" value="" />
									</td>
									<td>
										<select name="emblems">
											<option value="X">없음</option>
											<option value="N">NEW</option>
											<option value="E">EVENT</option>
											<option value="S">SPECIAL</option>
											<option value="H">HOT</option>
										</select>
									</td>
								</tr>
							</c:forEach> -->
						</tbody>
					</table>
					<input type="button" class="btn" value="취소" onclick="javascript:document.listFrm.submit();" />
					<input type="submit" class="btn" value="${fAct}" />
					<c:if test="${not empty brand}">
						<input type="button" class="btn" value="삭제" onclick="javascript:if(confirm('삭제 하시겠습니까?')){document.removeFrm.submit();}" />
					</c:if>
				</form>
				<form name="listFrm" action="<c:url value="/event/list.do" />" method="post" style="display: none;">
					<input type="hidden" name="maxnum" value="${param.maxnum}" />
					<input type="hidden" name="page" value="${param.page}" />
					<input type="hidden" name="searchDate" value="${param.searchDate}">
					<input type="hidden" name="startDate" value="${param.startDate}" /> 
					<input type="hidden" name="endDate" value="${param.endDate}" />
					<input type="hidden" name="name" value="${param.searchName}" />
				</form>
				
				<form name="removeFrm" action="<c:url value="/event/remove.do" />" method="post" style="display: none;">
					<input type="hidden" name="fAct" value="remove">
					<input type="hidden" name="idx" value="${event.idx}">
					<input type="hidden" name="maxnum" value="${param.maxnum}" />
					<input type="hidden" name="page" value="${param.page}" />
					<input type="hidden" name="searchDate" value="${param.searchDate}">
					<input type="hidden" name="startDate" value="${param.startDate}" /> 
					<input type="hidden" name="endDate" value="${param.endDate}" />
					<input type="hidden" name="searchName" value="${param.searchName}" />
				</form>
			</div>
<%@ include file="/WEB-INF/pages/includes/footer.jsp" %>
		</div>
	</body>
</html> 